using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.Data;

namespace ChairtyBank
    {
    public static class DBAccess
        {
        public static bool CreateBackup(string connectionString, string backUpFile)
        {
            SqlCommand CreateTableCommand = new SqlCommand();
            SqlConnection con = new SqlConnection(connectionString);
            CreateTableCommand.Connection = con;
            CreateTableCommand.CommandType = CommandType.Text;
            string dbName=GetDbNameFromConnectionString(connectionString);
            if (dbName != null)
                CreateTableCommand.CommandText = "USE MASTER;BACKUP DATABASE [" + dbName + "] TO  DISK = N'" + backUpFile + ".bak' WITH NOFORMAT, NOINIT,  NAME = N'" + dbName + "-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
            else 
                return false;
            try
            {
                CreateTableCommand.Connection.Open();
                CreateTableCommand.ExecuteNonQuery();
                CreateTableCommand.Connection.Close();
                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                con.Close();
            }
        }
         public static bool RestoreDB(string connectionString, string backUpFile)
        {
            SqlCommand CreateTableCommand = new SqlCommand();
            SqlConnection con = new SqlConnection(connectionString);
            CreateTableCommand.Connection = con;
            CreateTableCommand.CommandType = CommandType.Text;
            string dbName = GetDbNameFromConnectionString(connectionString);
            if (dbName != null)
                CreateTableCommand.CommandText = "USE MASTER; RESTORE DATABASE [" + dbName + "] FROM  DISK = N'" + backUpFile + "' WITH REPLACE";
            else
                return false;
            try
            {
                CreateTableCommand.Connection.Open();
                CreateTableCommand.ExecuteNonQuery();
                CreateTableCommand.Connection.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                con.Close();
            }
        }

         public static string GetDbNameFromConnectionString(string connectionString)
         {
             string result = null;
             string[] parts = connectionString.Split(';');
             if (parts.Length > 0)
             {
                 foreach (string s in parts)
                 {
                     if (s.Contains("Initial Catalog"))
                     {
                         result=s.Split('=')[1];
                     }
                 }
             }

             return result;

         }
        }
    }
